/**
 * Created with JetBrains WebStorm.
 * User: Eric
 * Date: 16/03/13
 * Time: 19:49
 * To change this template use File | Settings | File Templates.
 */

https://github.com/mikeal/request
    var url = "",
        express = require('express');

var app = express(),
    request = require('request'),
    assert = require("assert"),
    cheerio = require('cheerio');

describe('login page (/login)',function(){

    before(function(done){
        url = 'http://localhost:3010';
        done();
    });

    it('GET should return 200',function(done){
        request(url + "/login", function(error,response,body){
            assert.equal(response.statusCode, 200);
            done();
        });
    });

    it('GET should show a login form',function(done){
        request(url + "/login", function(error,response,body){
            var $ = cheerio.load(body);
            assert.equal($('form').attr('action'),"/login");
            done();
        });
    });

    it('POST should refuse bad submissions', function(done){

        request.post(url + "/login",{form:{fail: 'fail'}}, function(error,response,body){
            var $ = cheerio.load(body);
            assert.notEqual($('.field.error').length,0);
            done();
        })

    });

    it('POST should refuse partial submissions', function(done){
        request.post(url + "/login",{form:{email: 'test@test.com'}}, function(error,response,body){
            var $ = cheerio.load(body);
            assert.notEqual($('.field.error').length,0);
            done();
        })
    });

    it('POST should keep values on partial submissions', function(done){
        request.post(url + "/login",{form:{email: 'test@test.com'}}, function(error,response,body){
            var $ = cheerio.load(body);
            assert.equal($('#id_email').attr('value'),"test@test.com");
            done();
        })
    });

    it('POST should refuse invalid emails', function(done){
        request.post(url + "/login",{form:{email: 'badEmail'}}, function(error,response,body){
            var $ = cheerio.load(body);
            var emailInput = $('#id_email');
            var divEmail = $(emailInput).parent();
            assert.ok(divEmail.hasClass('error'));
            done();
        })
    });

});